<template>
    <div class="test-paper">
        <div class="test-paper-left">
            <div class="title">
                <div class="title-text">考生信息</div>
                <div class="title-item">
                    考号：
                    <span>{{ userStore.getUserinfo.username }}</span>
                </div>
                <div class="title-item">
                    姓名：
                    <span>{{ userStore.getUserinfo.realname }}</span>
                </div>
                <div class="title-item">
                    性别：
                    <span>{{ userStore.getUserinfo.sex }}</span>
                </div>
            </div>
            <div class="content topic">
                <!-- <div class="content-preview">
                    <img class="test-paper-logo" src="@assets/img/test-paper.png" />
                    <div class="paper-info">
                        <div class="paper-info-item">考试名称：水处理技术</div>
                        <div class="paper-info-item">考试时间：2024年3月21日10:00</div>
                        <div class="paper-info-num">
                            <div class="paper-info-num-item">单选题：30道</div>
                            <div class="paper-info-num-item">多选题：30道</div>
                            <div class="paper-info-num-item">简答题：30道</div>
                            <div class="paper-info-num-item">判断题：30道</div>
                        </div>
                    </div>
                </div> -->
                <div class="content-preview done" v-if="examStore.examFinished">
                    <img class="test-paper-logo" src="@assets/img/end-exam.png" />
                    <div class="paper-info">
                        <div class="paper-info-topic">答题完成！</div>
                        <div class="paper-info-item">
                            考试名称：{{ examStore.paperInfo.paperName }}
                        </div>
                        <div class="paper-info-item">
                            <!-- 答题时长：{{ examStore.answerTime }} -->
                            {{ countTime }}秒后退出
                        </div>
                        <div class="paper-info-num">
                            <div
                                v-for="(item, index) in examStore.getTestCardInfo"
                                :key="index"
                                class="paper-info-num-item"
                            >
                                {{ examTest[item.eqType] }}：{{ item.eqNum }}道
                            </div>
                        </div>
                    </div>
                </div>
                <Topic
                    v-else
                    class="exam-info"
                    @selectChange="selectChange"
                    :currentAnswer="currAnswer"
                ></Topic>

                <div class="content-footer" v-if="!examStore.examFinished">
                    <!-- <el-button class="footer-btn">返回试卷</el-button> -->
                    <!-- <el-button type="primary" class="footer-btn start-test" @click="startAnswer">开始答题</el-button> -->
                    <!-- <el-button type="danger" class="footer-btn">检查试卷</el-button> -->
                    <el-button
                        v-if="examStore.getCurrentExamIndex >= 1"
                        class="footer-btn"
                        @click="prevExam"
                    >
                        上一题
                    </el-button>
                    <el-button
                        v-if="examStore.getCurrentExamIndex != examNum - 1"
                        type="primary"
                        class="footer-btn start-test"
                        @click="nextExam"
                    >
                        下一题
                    </el-button>
                </div>
                
            </div>
        </div>
        <div class="test-paper-card">
            <PaperCard></PaperCard>
        </div>
    </div>
</template>

<script setup>
import PaperCard from './components/paperCard.vue'
import Topic from './components/topic.vue'
import { useUserinfoStore } from '@/stores/user'
import { useExamStore } from '@/stores/exam'
import { ref, computed, onUnmounted, watchEffect, onMounted } from 'vue'
import { submitAnswer, getCurrentExam, getCurrentExamData } from '@/api/login'
import { useRoute } from 'vue-router'
import { ElMessage } from 'element-plus'
import { getExamTime } from '@/api/login'

const route = useRoute()
const userStore = useUserinfoStore()
const examStore = useExamStore()

let examLocalData = JSON.parse(sessionStorage.getItem('examLocalData'))
if (examLocalData) {
    examStore.initExamListData(examLocalData)
}

let examTest = {
    1: '单选题',
    2: '多选题',
    3: '判断题',
    4: '填空题',
    5: '简答题',
    6: '综合题'
}
let examNum = ref(0)
examNum.value = examStore.paperInfo.questionTotalNum

// 选择答案，设置缓存到本地
async function selectChange(answer) {
    debugger
    let res = await submitAnswer({
        sbId: route.query.sbId,
        eqId: answer.eqId,
        answer: answer.eqoId,
    })
    if (res.data.code === 0) {
        examStore.setAnswer(answer)
    }
}
let countTime = ref(3)

watchEffect(() => {
    if (examStore.examFinished) {
        let idx = setInterval(() => {
            countTime.value --
            if (countTime.value === 0) {
                clearInterval(idx)
                sessionStorage.removeItem('token')
                sessionStorage.removeItem('user')
                location.reload()
            }
        }, 1000)
    }
})

onMounted(() => {
    handlePaperHistory()
})
// 请求正在答题的数据，以便设置回显
async function handlePaperHistory() {
    let currExamRes = await getCurrentExam({
        sbId: route.query.sbId,
    })

    if (currExamRes.data.code === 0) {
        if (currExamRes.data.data.examStatus === '1') {
            examStore.examFinished = true
        } else {
            if (currExamRes.data.answerInfo.length > 0) {
                examStore.clearUserAnswer()
                currExamRes.data.answerInfo.forEach(item => {
                    examStore.setAnswer({
                        eqId: item.eqId,
                        eqoId: item.answer
                    })
                })
            }
            examStore.paperInfo.answerStartTime = currExamRes.data.data.answerStartTime
            examStore.paperInfo.answerEndTime = currExamRes.data.data.answerEndTime
            examStore.paperInfo.durationTime = currExamRes.data.data.durationTime
            
            serverTimeProofread()
        }
        
    }
}

// 离开试卷页面，清除定时器
onUnmounted(() => {
    examStore.cleanTimeout()
})

randomTimeGet()

//服务器时间 1-3分钟 不定时校对
function randomTimeGet() {
    setTimeout(function myTimer() {
        if (!examStore.examFinished) {
            serverTimeProofread()
            let timeoutIndex = setTimeout(myTimer, Math.floor(Math.random() * 3) * 60 * 1000)
            console.log('timeoutIndex', timeoutIndex)
            examStore.timeoutIndex = timeoutIndex
        }
    }, Math.floor(Math.random() * 3) * 60 * 1000)
}

// 服务器时间校对
async function serverTimeProofread() {
    let res = await getExamTime()
    let serverTime = res.data.data

    let answerEndTime = examStore.paperInfo.answerEndTime
    let reminder = new Date(answerEndTime).getTime() - serverTime
    if (reminder <= 0) {
        examStore.reminder = 0
        examStore.examFinished = true
    } else {
        examStore.reminder = reminder
        examStore.examStart = true
        examStore.showTime = true
    }
}

let currAnswer = ref('')
// 下一题
function nextExam() {
    examStore.currentExamIndex++
}

// 上一题
function prevExam() {
    examStore.currentExamIndex--
}

</script>

<style lang="less" scoped>
.radius() {
    box-shadow: 0px 5px 9px 0px rgba(57, 126, 253, 0.0784);
    border-radius: 15px 15px 15px 15px;
}
.test-paper {
    font-size: 16px;
    display: flex;
    justify-content: space-between;
    &-left {
        width: 1460px;
        .title {
            display: flex;
            align-items: center;
            background-color: #fff;
            margin-bottom: 20px;
            padding-left: 30px;
            height: 75px;
            .radius();
            &-text {
                margin-right: 75px;
                font-weight: 700;
                font-size: 24px;
                color: #333333;
                padding-left: 15px;
                border-left: 8px solid #397efd;
            }
            &-item {
                margin-right: 75px;
                font-size: 24px;
                color: #333;
                span {
                    color: #397efd;
                }
            }
        }
        .content.topic {
            justify-content: flex-start;
            padding: 44px 56px;
            .exam-info {
                width: 100%;
                height: calc(100% - 70px);
                overflow-y: auto;
            }
        }
        .content {
            position: relative;
            display: flex;
            justify-content: center;
            height: calc(100% - 95px);
            background-color: #fff;
            overflow: hidden;
            .radius();
            &-preview {
                display: flex;
                align-items: center;
                width: 800px;
                height: 500px;
                margin: 0 auto;
                .test-paper-logo {
                    width: 358px;
                    height: 212px;
                }
                .paper-info {
                    margin-left: 30px;
                    width: 450px;
                    &-item {
                        margin-bottom: 45px;
                        font-weight: 700;
                        font-size: 27px;
                        color: #333333;
                    }
                    &-num {
                        display: flex;
                        flex-wrap: wrap;
                        justify-content: space-between;
                        &-item {
                            margin-bottom: 20px;
                            width: 200px;
                        }
                    }
                }
            }
            &-preview.done {
                .test-paper-logo {
                    margin-right: 40px;
                    width: 281px;
                    height: 281px;
                }
                .paper-info-topic {
                    margin-bottom: 40px;
                    font-weight: 700;
                    font-size: 54px;
                    color: #00c882;
                }
            }
            &-footer {
                display: flex;
                justify-content: right;
                align-items: center;
                position: absolute;
                padding-right: 45px;
                bottom: 0;
                left: 0;
                right: 0;
                height: 107px;
                border-top: 1px solid #d8d8d8;
                background-color: #fff;
                .start-test {
                    background-color: #397efd;
                }
                .footer-btn {
                    width: 180px;
                    height: 56px;
                }
            }
        }
    }
    &-card {
        width: 370px;
        background-color: #fff;
        .radius();
    }
}
</style>
